﻿using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using NLog;

namespace Quadrant.web.Helpers
{
    public abstract class BaseApiController : ApiController
    {
        private Guid _userId;

        protected Guid UserId
        {
            get
            {
                if (this._userId == Guid.Empty)
                {
                    this._userId = new Guid(this.User.Identity.GetUserId());
                }
                return this._userId;
            }
        }

        protected BaseMvcApplication CurrentApplication
        {
            get { return HttpContext.Current.ApplicationInstance as BaseMvcApplication; }
        }

        protected Logger Logger
        {
            get { return this.CurrentApplication.Logger; }
        }
    }

    public abstract class BaseController : Controller
    {
        private Guid _userId;

        protected Guid UserId
        {
            get
            {
                if (this._userId == Guid.Empty)
                {
                    this._userId = new Guid(this.User.Identity.GetUserId());
                }
                return this._userId;
            }
        }

        protected BaseMvcApplication CurrentApplication
        {
            get { return HttpContext.ApplicationInstance as BaseMvcApplication; }
        }

        protected Logger Logger
        {
            get { return this.CurrentApplication.Logger; }
        }

        //protected override void OnException(ExceptionContext filterContext)
        //{
        //    var controllerName = (string)filterContext.RouteData.Values["controllers"];
        //    var actionName = (string)filterContext.RouteData.Values["action"];
        //    var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
        //    const string viewName = "Error";
        //    var viewData = new ViewDataDictionary<HandleErrorInfo>(model);
        //    var tempData = filterContext.Controller.TempData;
        //    if (filterContext.IsChildAction || filterContext.HttpContext.Request.IsAjaxRequest())
        //    {
        //        filterContext.Result = new PartialViewResult
        //        {
        //            ViewName = viewName,
        //            ViewData = viewData,
        //            TempData = tempData
        //        };
        //    }
        //    else
        //    {
        //        filterContext.Result = new ViewResult
        //        {
        //            ViewName = viewName,
        //            //MasterName = Master,
        //            ViewData = viewData,
        //            TempData = tempData
        //        };
        //    }
        //    int httpCode = 500; //code par défaut, le plus général possible
        //    var exception = filterContext.Exception;
        //    if (exception is HttpException || exception.GetBaseException() is HttpException)
        //    {
        //        var httpException = exception as HttpException ?? exception.GetBaseException() as HttpException;
        //        if (httpException != null) httpCode = httpException.GetHttpCode();
        //    }
        //    filterContext.HttpContext.Response.Clear();
        //    filterContext.ExceptionHandled = true;
        //    filterContext.HttpContext.Response.StatusCode = httpCode;
        //    filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
        //}

    }

    public class ErrorController : Controller
    {
        private IAuthenticationManager AuthenticationManager
        {
            get
            {
                return HttpContext.GetOwinContext().Authentication;
            }
        }

        public ActionResult Index(string id)
        {
            this.AuthenticationManager.SignOut();
            ViewBag.ErrorId = id;
            return View();
        }
    }
}